package com.company;

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.security.*;
import org.apache.commons.codec.digest.DigestUtils;

public class Main {

    private final static Map<Integer,String> map = getMap();

    public static String getMD5Utils(String str)
    {
        if (str.isEmpty()) { return ""; }

        String sign =   DigestUtils.md5Hex(str);
        return sign;
    }

    public static String getSHA1Utils(String str)
    {
        if (str.isEmpty()) { return ""; }

        String sign = DigestUtils.sha1Hex(str);
        return sign;
    }

    public static void main(String[] args) throws  NoSuchAlgorithmException
    {
        //ListTest();
        //System.out.println();
        // DictTest();

        String str="ROSE123";

        System.out.println("原字符串：" + str);

        System.out.println("---MD5---");
        String md5Text=getHashText(str,"MD5");  ///String md5Text=getHashText(str,map.get(5));//key:5
        System.out.println(md5Text);

        String md5Text2=getMD5(str);
        System.out.println(md5Text2);

        String md5Text3=getMD5Utils(str);
        System.out.println(md5Text3);

        System.out.println("---SHA1---");
        String sha1Text=getSHA1(str);
        System.out.println(sha1Text);

        String sha1Text2=getSHA1Utils(str);
        System.out.println(sha1Text2);
        /*
efd86c891eaa89b9112bd240919d276161405270
efd86c891eaa89b9112bd240919d276161405270
        */
    }

    private  static  Map<Integer,String> getMap()
    {
        Map<Integer,String> map = new HashMap<Integer, String>() ;
        map.put(5,"MD5");
        map.put(1,"SHA1");

        return  map;
    }

    public static String getMD5(String str) throws  NoSuchAlgorithmException
    {
        if (str.isEmpty()){return "";}

        String result=getHashText(str,"MD5");
        return  result;
    }
    public static String getSHA1(String str) throws  NoSuchAlgorithmException
    {
        if (str.isEmpty()){return "";}

        String result=getHashText(str,"SHA1");
        return  result;
    }

    protected  static String getHashText(String str,String hashType) throws  NoSuchAlgorithmException
    {
        if (str.isEmpty() || hashType.isEmpty()) { return ""; }

        MessageDigest md = MessageDigest.getInstance(hashType);
        byte[] result=md.digest(str.getBytes());
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < result.length; i++)
        {
            sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }



    //集合
    private static void ListTest()
    {
        int i = 1;
        String str = "ROSE";
        Date date = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);

        List<Object> list = new ArrayList<Object>();
        list.add(i);
        list.add(str);
        list.add(date);
        list.add(time);

        for (Object obj : list)
        {
            System.out.println(obj);
        }
    }

    //字典
    private static void DictTest()
    {
        int i = 1;
        String str = "ROSE";
        Date date = new Date();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FID", i);
        map.put("FName", str);
        map.put("FAddTime", date);

        for (Map.Entry<String, Object> entry : map.entrySet())
        {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
    }
}
